/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.dispatcher
{
	import com.autoswf.core.Transport;
	
	import flash.utils.Dictionary;


	/**
	 * Upstream node for directing events traveling up the tree to interested leafs.
	 */
	public class RemoteDispatcher extends LinkDispatcher
	{
		/**
		 * Constructor.
		 */
		public function RemoteDispatcher(id:String)
		{
			super(id);
		}
		
		
		/** Checks for Listeners traversing the graph to add them to this event dispatcher. */
		override public function sendUp(object:*):void
		{
			var transport:Transport = (object is Transport) ? object : new Transport(object);
			
			if(transport is ListenerTransport)
			{
				var listener:ListenerTransport = ListenerTransport(transport);
				if(listener.removeListener) /* If the arguments are for removing a listener */
				{
					_dispatcher.removeEventListener.apply(null, listener.arguments);
				}
				else /* the arguments are for adding a listener */
				{
					_dispatcher.addEventListener.apply(null, listener.arguments);
				}
				return;
			}
			
			super.sendUp(transport);
		}
		
		
		/** Convenience method for adding a listener to a remote dispatcher. */
		public function addRemoteListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.sendUp(new ListenerTransport(arguments));
		}
		
		
		/** For removing a listener from a remote dispatcher. */
		public function removeRemoteListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.sendUp(new ListenerTransport(arguments, true));
		}
	}
}